package com.hns.can.master.web;

import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hns.can.base.entity.SysUserEntity;
import com.hns.can.common.domain.GridPage;
import com.hns.can.common.entity.BaseEntity;
import com.hns.can.common.web.BaseController;
import com.hns.can.master.entity.BaseLineCarRelEntity;
import com.hns.can.master.service.LineCarRelService;
import com.hns.can.master.vo.LineCarRelParamsVo;
import com.hns.frame.enumrate.MsgType;
import com.hns.frame.exception.BusinessException;
import com.hns.frame.mybatis.Pagination;
import com.hns.tool.export.ExportUtil;
import com.hns.tool.pub.PubUtil;

/**
 * 线路车辆分配的控制类
 * @author:lan
 * @email:763836737@qq.com
 * @version Revision 2.0.0
 */
@Controller
@RequestMapping("/lineCarRel")
public class LineCarRelController extends BaseController{
	/**
	 * 线路车辆分配的service
	 */
    @Autowired
    LineCarRelService lineCarRelService;
    
    /**
	 * 导出文件表头
	 */
    private final String[] tableHeaderNames = {"线路名称", "车牌号", "设备号", "车辆类型", "运营证号", "驾驶员绑定状态", "备注" };
    /**
	 * 导出文件字段名
	 */
    private final String[] tableHeaderFields = {"lineName", "licPltNo", "eqmtCd", "carType", "operCertiNo", "binding", "remark" };
    
    /**
     * 获取线路车辆绑定关系
     * @param entity
     * @return
     * @throws Exception
     * @author:lan
     * @email:763836737@qq.com
     */
    @RequestMapping("/searchLineCarRel")
    @ResponseBody
    public Object searchLineCarRel(LineCarRelParamsVo entity) throws Exception{
        SysUserEntity user = this.getCurrentUser().getUser();
        if ((PubUtil.isEmpty(entity.getLineIds()) || entity.getLineIds().length <= 0) && (PubUtil.isEmpty(entity.getCarIds()) || entity.getCarIds().length <= 0)) {
            return this.getReturnResult();
        } 
        List<String> orgIds = null;
        if(PubUtil.isNotEmpty(entity.getLineIds())&&entity.getLineIds().length>0){
            orgIds = Arrays.asList(entity.getLineIds());
        }
        if(PubUtil.isNotEmpty(entity.getCarIds())&&entity.getCarIds().length>0){
            orgIds = Arrays.asList(entity.getCarIds());
        }
        entity.setCorpId(user.getCorpId());
        Pagination page = lineCarRelService.getLineCarRel(orgIds, entity);
        GridPage grid = new GridPage(page);
        return this.getReturnResult(grid);
    }
    
    /**
     * 获取未与任何线路关联的车辆
     * @param entity
     * @return
     * @throws Exception
     * @author:lan
     * @email:763836737@qq.com
     */
    @RequestMapping("/getUnboundCar")
    @ResponseBody
    public Object getUnboundCar(BaseLineCarRelEntity entity) throws Exception{
        if ((PubUtil.isEmpty(entity.getLineId()))) {
            throw new BusinessException(MsgType.WARN.getFlag(), "请选择线路或者车辆");
        } else {
            SysUserEntity user = this.getCurrentUser().getUser();
            Pagination page = lineCarRelService.getUnboundCar(entity, user.getCorpId());
            GridPage grid = new GridPage(page);
            return this.getReturnResult(grid);
        }
    }
    
    /**
     * 添加线路车辆关联 
     * @param corpIds
     * @param lineId
     * @param carIds
     * @return
     * @throws Exception
     * @author:lan
     * @email:763836737@qq.com
     */
    @RequestMapping("/saveLineCarRel")
    @ResponseBody
    public Object saveLineCarRel(@RequestParam(value = "corpIds") String[] corpIds, 
        @RequestParam(value = "lineId") String lineId, 
        @RequestParam(value = "carIds") String[] carIds) throws Exception {
        SysUserEntity user = this.getCurrentUser().getUser();
        lineCarRelService.saveLineCarRel(user.getId(), lineId, carIds, corpIds);
        return this.getReturnMsg("保存成功");
    }
    
    /**
     * 删除线路车辆关系
     * @param relId
     * @param carId
     * @param licPltNo
     * @return
     * @author:lan
     * @email:763836737@qq.com
     */
    @RequestMapping("/delLineCarRel")
    @ResponseBody
    public Object delLineCarRel(@RequestParam(value = "relId") String relId, 
        @RequestParam(value = "carId",required = false)String carId,
        @RequestParam(value = "licPltNo",required = false)String licPltNo) {
        int drvNum = getBindingStatus(carId);
        if(drvNum>0){
            throw new BusinessException(MsgType.WARN.getFlag(), licPltNo+"已绑定"+drvNum+"名驾驶员!<br/>不能解除该车!<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;请先解除该车的驾驶员绑定!");
        }else{
            lineCarRelService.delLineCarRel(relId);
            return this.getReturnMsg("删除成功");
        }
    }
    
    /**
     * 导出excel
     * @param entity
     * @param response
     * @throws Exception
     * @author:lan
     * @email:763836737@qq.com
     */
    @RequestMapping("/exportLineCarRelExcel")
    @ResponseBody
    public void exportLineCarRelExcel(LineCarRelParamsVo entity, HttpServletResponse response) throws Exception {
        SysUserEntity user =this.getCurrentUser().getUser();
        List<String> orgIds = null;
        if(PubUtil.isNotEmpty(entity.getLineIds())&&entity.getLineIds().length>0){
            orgIds = Arrays.asList(entity.getLineIds());
        }
        if(PubUtil.isNotEmpty(entity.getCarIds())&&entity.getCarIds().length>0){
            orgIds = Arrays.asList(entity.getCarIds());
        }
        entity.setCorpId(user.getCorpId());
        List<BaseEntity> list = lineCarRelService.getAllLineCarRel(orgIds, entity);
        ExportUtil.exportFiles(Arrays.asList(tableHeaderNames), Arrays.asList(tableHeaderFields), list, response, "excel", "lineCarRel", "线路车辆关系");
    }
    
    /**
     * 导出pdf
     * @param entity
     * @param response
     * @throws Exception
     * @author:lan
     * @email:763836737@qq.com
     */
    @RequestMapping("/exportLineCarRelPdf")
    @ResponseBody
    public void exportLineCarRelPdf(LineCarRelParamsVo entity, HttpServletResponse response) throws Exception {
        SysUserEntity user =this.getCurrentUser().getUser();
        List<String> orgIds = null;
        if(PubUtil.isNotEmpty(entity.getLineIds())&&entity.getLineIds().length>0){
            orgIds = Arrays.asList(entity.getLineIds());
        }
        if(PubUtil.isNotEmpty(entity.getCarIds())&&entity.getCarIds().length>0){
            orgIds = Arrays.asList(entity.getCarIds());
        }
        entity.setCorpId(user.getCorpId());
        List<BaseEntity> list = lineCarRelService.getAllLineCarRel(orgIds,entity);
        ExportUtil.exportFiles(Arrays.asList(tableHeaderNames), Arrays.asList(tableHeaderFields), list, response, "pdf", "lineCarRel", "线路车辆关系");
    }
    
    /**
     * 获取人车绑定状态
     * @param carId
     * @return
     * @author:lan
     * @email:763836737@qq.com
     */
    private int getBindingStatus(String carId){
        return lineCarRelService.getCarDriverBinding(carId);
    }
}
